/**
 * 创建日期:2010
 */
package com.umpay.privi.util;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * PO 转换为 Map 工具类
 * 
 * @author 弓勤
 */
@SuppressWarnings("unchecked")
public class ObjectUtil {

	/**
	 * 对象转map
	 * 
	 * @param obj
	 *            要转换的对象
	 * @param mask
	 *            其中不需要的属性，注意用逗号隔开如"属性1，属性2"
	 * @return Map<String,Object>映射
	 */
	public static Map<String, Object> object2Map(Object obj, String mask) {
		Map<String, Object> result = new HashMap<String, Object>();
		Field[] fields = obj.getClass().getDeclaredFields(); // 取得全部的域
		
		for (Field field : fields) {
			field.setAccessible(true); // 设置可以读写
			if (!Modifier.isStatic(field.getModifiers()) && Modifier.isPrivate(field.getModifiers())) {
				try {
					Object myobj = field.get(obj);
					if (myobj != null) { // 如果非空加入对象
						if (myobj instanceof String) {
							result.put(field.getName(), ((String)myobj).trim());
						} else {
							result.put(field.getName(), myobj);
						}
					} else {
						result.put(field.getName(), ""); // 如果空加入空字符串
					}
				} catch (Exception e) {
					continue;
				}
			}
			
		}

		return result;
	}
	/**
	 * 将对象转换为map
	 * @param obj 需转换的对象
	 * @param neededKey 需要放入map中的主键值,注意用逗号隔开如"属性1，属性2"
	 * @return
	 * @see:
	 */
	public static Map<String, String> object2StrMap(Object obj, String neededKey) {
		Field[] fields = obj.getClass().getDeclaredFields(); // 取得全部的域
		Map<String, String> result = new HashMap<String, String>();
		for (Field field : fields) {
			field.setAccessible(true); // 设置可以读写
			if (!Modifier.isStatic(field.getModifiers()) && Modifier.isPrivate(field.getModifiers())) {
				try {
					Object myobj = field.get(obj);
					String keyStr = field.getName();
					if (neededKey.indexOf(keyStr) >= 0) {
						if (myobj != null) { // 如果非空加入对象
							if (myobj instanceof String) {
								result.put(keyStr, ((String)myobj).trim());
							} else {
								result.put(keyStr, myobj.toString());
							}
						} else {
							result.put(field.getName(), ""); // 如果空加入空字符串
						}
					}
					
				} catch (Exception e) {
					continue;
				}
			}
			
		}
		return result;
	}
	
	public static Map<String, Object> object2Map(Object obj) {
		return object2Map(obj,"");
	}
	
	/**
	 * 对象转换为日志信息  格式为： name:test,gender:1
	 * @param obj
	 * @return
	 * @return:
	 */
	public static String object2LogStr(Object obj) {
		
		StringBuffer logStrBuffer = new StringBuffer();
		Map<String, Object> paraMap = object2Map(obj);
		Iterator it = paraMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			if (value != null && value.toString().length() > 0) {
				logStrBuffer.append(key.toString()).append(":").append(value.toString()).append(" ");
			}
		}
		
		return logStrBuffer.toString();
	}
	
	public static String object2LogStr(Object obj,String mask) {
		mask = StringUtil.trim(mask);
		Map<String, String> maskMap = new HashMap<String, String>();
		if (!"".equals(mask)) {
			String[] maskArr = mask.split(",");
			for (int i = 0; i < maskArr.length; i++) {
				maskMap.put(StringUtil.trim(maskArr[i]), StringUtil.trim(maskArr[i]));
			}
		}
		
		StringBuffer logStrBuffer = new StringBuffer();
		Map<String, Object> paraMap = object2Map(obj);
		Iterator it = paraMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			Object key = entry.getKey();
			String keyStr = StringUtil.trim(key.toString());
			if (!maskMap.containsKey(keyStr)) {
				Object value = entry.getValue();
				if (value != null && value.toString().length() > 0) {
					logStrBuffer.append(keyStr).append(":").append(value.toString()).append(" ");
				}
			}
		}
		
		return logStrBuffer.toString();
	}

	/**
	 * 把list转为带转换为map的对象列表
	 * 
	 * @param list
	 *            需转换列表
	 * @param mask
	 *            屏蔽的域
	 * @return List<Map<String,Object>>格式的列表。
	 */
	public static List<Map<String, Object>> list2MapList(List list, String mask) {
		if (list == null || list.isEmpty() || list.size() == 0)
			return null;
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		for (Object obj : list) {
			result.add(object2Map(obj, mask));
		}
		return result;
	}
	
	public static String list2logStr(List list) {
		if (list == null || list.isEmpty() || list.size() == 0)
			return "[]";
		StringBuffer resultStr = new StringBuffer();
		resultStr.append("[");
		for (Object obj : list) {
			resultStr.append("{").append(obj.toString()).append("},");
		}
		return resultStr.append("]").toString();
	}
	
	public static void setObjByMap(Object obj, Map<String, Object> paraMap) {
		Field[] fields = obj.getClass().getDeclaredFields(); // 取得全部的域
		for (Field field : fields) {
			field.setAccessible(true); // 设置可以读写
			try {
				String fieldNameTmp = field.getName().trim();
				if (paraMap.get(fieldNameTmp) != null) {
					field.set(obj, paraMap.get(fieldNameTmp));
				}
			} catch (Exception e) {
				continue;
			}
		}
	}
	
	public static boolean isWrapClass(Class clz) { 
        try { 
           return ((Class) clz.getField("TYPE").get(null)).isPrimitive();
        } catch (Exception e) { 
            return false; 
        } 
    } 
}
